﻿using Atlanchim.Crm.Mui.Helpers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Atlanchim.Crm.Mui.Commands
{
    public class AdminCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (parameter != null)
            {
                string command = parameter.ToString().ToLowerInvariant();

                if (!string.IsNullOrEmpty(command))
                {
                    AdminCommandHelper.Execute(command);
                }
            }
        }

        public static void SetConnectionString(string newConnectionString)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConnectionStringsSection cnxSection = config.ConnectionStrings;

            cnxSection.ConnectionStrings.Remove("CrmContext");
            ConnectionStringSettings newCnx = new ConnectionStringSettings("CrmContext", newConnectionString);
            newCnx.ProviderName = "System.Data.EntityClient";

            //cnxSection.ConnectionStrings["CrmContext"].ConnectionString = newConnectionString;
            cnxSection.ConnectionStrings.Add(newCnx);
            config.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection("ConnectionStrings");
        }
    }
}
